package com.ruoyi.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.client.domain.entity.BlogArticle;
import com.ruoyi.client.domain.entity.BlogTags;
import com.ruoyi.client.mapper.BlogTagsMapper;
import com.ruoyi.client.service.IBlogTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author fz
 * @since 2025-02-27
 */
@Service
public class BlogTagsServiceImpl extends ServiceImpl<BlogTagsMapper, BlogTags> implements IBlogTagsService {

    @Override
    public TableDataInfo<BlogTags> selectBlogTagsList(PageQuery pageQuery) {
        Page<BlogTags> page = this.page(pageQuery.build(),buildWrapper(pageQuery.getParams()));
        return TableDataInfo.build(page);
    }

    @Override
    public List<BlogTags> getBlogTagsByHot() {
        List<BlogTags> list = list(Wrappers.<BlogTags>lambdaQuery()
            .eq(BlogTags::getCustomHot, 1)
            .eq(BlogTags::getStatus, 1)
        );
        return list;
    }

    private Wrapper<BlogTags> buildWrapper(Map<String, Object> params) {
        QueryWrapper<BlogTags> wrapper = new QueryWrapper<>();
        if (CollectionUtils.isEmpty(params)) return wrapper;

        Set<Map.Entry<String, Object>> set = params.entrySet();
        for (Map.Entry<String, Object> entry : set) {
            String field = entry.getKey();
            Object value = entry.getValue();

            if (value != null) {
                // 添加等值条件
                if (field.equals("tagType")) {
                    wrapper.eq(field,value);
                }else {
                    wrapper.like(field,value);

                }
            }
        }
        return wrapper;
    }
}
